﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace COFE.Core.UnitTest
{
    public class TempDirectoryConstructor : ITempDirectoryConstructor
    {
        public static TempDirectoryConstructor Instance = new TempDirectoryConstructor();
        public static string TempPath = @"C:\Temp\COFE2";

        #region Constructor

        static TempDirectoryConstructor()
        {
            Directory.CreateDirectory(TempPath);
        }

        #endregion

        public static List<string> tempDirectory = new List<string>();

        ~TempDirectoryConstructor()
        {
            try
            {
                if (tempDirectory != null)
                {
                    lock (tempDirectory)
                        foreach (var p in tempDirectory)
                            Directory.Delete(p, true);
                }
            }
            catch
            {

            }
            finally
            {
                tempDirectory = null;
            }
        }

        public DirectoryInfo NewTemp()
        {
            int idx = new Random().Next(Int16.MaxValue);
            //while (Directory.Exists(Path.Combine(TempPath, idx.ToString())))
            //    idx++;
            string dirPath = Path.Combine(TempPath, idx.ToString());
            Directory.CreateDirectory(dirPath);

            lock (tempDirectory)
                tempDirectory.Add(dirPath);

            return new DirectoryInfo(dirPath);
        }

        private static byte[] EmptyZip = new byte[] { 0x50, 0x4B, 0x05, 0x06, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 }; //PK
        private static byte[] Empty7z = new byte[] {0x37,0x7A,0xBC,0xAF,0x27,0x1C,0x00,0x02,0x8D,0x9B,0xD5,0x0F,0x00,0x00,0x00,0x00,
                                         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; //7z

        public DirectoryInfo ConstructDirectory()
        {
            Action<string> createEmptyZip = (path) =>
            {
                if (!File.Exists(path))
                    using (BinaryWriter newFile = new BinaryWriter(File.Create(path)))
                    {
                        newFile.Write(EmptyZip);
                    }
            };

            Action<string> createEmpty7z = (path) =>
            {
                if (!File.Exists(path))
                    using (BinaryWriter newFile = new BinaryWriter(File.Create(path)))
                    {
                        newFile.Write(Empty7z);
                    }

            };

            Action<string> createTxtFile = (path) =>
            {
                if (!File.Exists(path))
                    using (var sw = System.IO.File.CreateText(path))
                        sw.WriteLine(path);
            };

            Func<DirectoryInfo, string, DirectoryInfo> createSubdir = (root, name) =>
                {
                    var retVal = root.CreateSubdirectory(name);
                    for (int i = 1; i <= 3; i++)
                        createTxtFile(Path.Combine(root.FullName, name, name + i.ToString() + ".txt"));
                    createEmptyZip(Path.Combine(root.FullName, name, name + ".zip"));
                    createEmpty7z(Path.Combine(root.FullName, name, name + ".7z"));

                    return retVal;
                };

            var temp = NewTemp();
            var a = createSubdir(temp, "a");
            var aa = createSubdir(a, "aa");
            var ab = createSubdir(a, "ab");
            var aba = createSubdir(ab, "aba");
            var abb = createSubdir(ab, "abb");
            var abc = createSubdir(ab, "abc");
            var ac = createSubdir(a, "ac");
            var b = createSubdir(temp, "b");
            var c = createSubdir(temp, "c");

            return temp;
        }
    }
}
